import sys
import os

# 添加当前目录到系统路径
if hasattr(sys, '_MEIPASS'):  # 打包环境
    sys.path.append(sys._MEIPASS)
else:  # 开发环境
    sys.path.append(os.path.abspath('.'))

from PyQt5.QtWidgets import QApplication, QDialog
from database.db_connector import DatabaseConnector
from gui.login_window import LoginWindow
from gui.admi.admin_dashboard import AdminDashboard
from gui.user.client_dashboard import ClientDashboard


def main():

    if hasattr(sys, '_MEIPASS'):
        os.chdir(sys._MEIPASS)

    app = QApplication(sys.argv)
    db = DatabaseConnector()

    # 显示登录窗口
    login_dialog = LoginWindow(db)
    if login_dialog.exec_() == QDialog.Accepted:
        user_type, user_id = login_dialog.authenticate()

        if user_type == "管理员":
            # 打开管理员界面
            admin_window = AdminDashboard(db, user_id)
            admin_window.show()
        else:
            # 打开客户界面
            client_window = ClientDashboard(db, user_id)
            client_window.show()

        sys.exit(app.exec_())
    else:
        sys.exit(0)


if __name__ == "__main__":
    main()